#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>

#define SPACE 's'
#define RED 'r'
#define BLUE 'b'
#define GREEN 'g'

#define DEBUG 0

// Flag vector to signal the end of the wins in the wins array
#define END_OF_WINS 0	

int columns;
int rows;
// NOTE: last_move starts at 1, not 0. This happens to be beneficial because, with padding, the first column is board[1]
int last_move;
int total_time;
int player_1_time;
int last_move_time;

// The Wins Lookup Table
char WINS_LOOKUP[12][4] = {"ggrr","grgr","rgrg","rrgg","grrg","rggr","ggbb","gbgb","bgbg","bbgg","gbbg","bggb"};

int h_lookup[2000] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,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,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

FILE *f;



/*takes wins as a n by 4 2D array and looks at the different wins and evaluates which player won
and their point values

This function has to do 3 things:
1) figure out which player won - return positive or negative values
2) Figure out what point value they won with - arrangment of the peices
3) If two players have the same winning value - return 1 point
*/
int evalWin(char wins[150][4])
{
	
	int i = 0;
	int points = 0;
	int bestwin = 0;
	int worstwin = 0;
	int redpoints = 0;
	int bluepoints = 0;
	do{
		////fprintf(f, f, "checking case %s\n", wins[i]);
		//check for tie - if there is nothing in the wins array
		if(wins[0][0] == '\0' || wins[0][0] == 0)
		{
			////fprintf(f, "0");//no wins in array - no nothing in array
			points = 0;
			return 0;
		}
		else//we have at least one win - check it out
		{
			//first - check for the good wins - where the first two char's will match
			if(wins[i][0] == wins[i][1])//same colour first two pieces
			{
				
				if(wins[i][0] == 'r')
				{
					redpoints += 5;
					points = -5;
				}//we won
				else if(wins[i][0] == 'b')
				{
					bluepoints += 5;
					points = 5;
				}
				
				else if(wins[i][0] == 'g')//don't know who won yet
				{
					if(wins[i][2] == 'r')
					{
						redpoints += 5;
						points = -5;
					}//we won
					else
					{
						bluepoints += 5;
						points = 5;
					}
				}
			}
			
			else if(wins[i][1] == wins[i][2])//middle two are same colour - 4 points
			{
				
				if(wins[i][0] == 'r')
				{
					redpoints += 4;
					points = -4;
				}//we won
				else if(wins[i][0] == 'b')
				{
					bluepoints += 4;
					points = 4;
				}
				else//first piece is green
				{//check the second piece
					if(wins[i][1] == 'r')
					{
						redpoints += 4;
						points = -4;
					}//we won  
					else if(wins[i][1] == 'b')
					{
						bluepoints += 4;
						points = 4;
					}
					
				}
				
			}
			else //3 points will be awarded
			{
				if(wins[i][0] == 'r')
				{
					redpoints += 3;
					points = -3;
				}//we won
				else if(wins[i][0] == 'b')
				{
					bluepoints += 3;
					points = 3;
				}
				else//first piece is green
				{//check the second piece
					if(wins[i][1] == 'r')
					{
						redpoints += 3;
						points = -3;
					}//we won
					else if(wins[i][1] == 'b')
					{
						bluepoints += 3;
						points = 3;
					}
				}
			}
			
			
			
		}
		if(points > 0)
		{
			if(bestwin < points)
				bestwin = points;
		}
		else
			if(worstwin > points)
			worstwin = points;
		
		i++;
	}while(wins[i][0] != 0);
	
	
	if( redpoints - bluepoints > 0)
		points = worstwin;
	else if(redpoints - bluepoints == 0)
		points = 1;
	else
		points = bestwin;
	
	////fprintf(f,  "%d\n", points);
	return points;
}

// (7,6,3,600,290,20,r,b,s,s,s,s,b,b,r,g,s,s,b,r,b,b,g,s,r,g,r,b,s,s,r,r,b,r,r,s,s,s,s,s,s,s,s,s,s,s,s,s)
int readBoard(char board[columns+2][rows+2])
{
	int i, j, p = 0;
	
	// Populate board array
	for (i = 0; i < columns+2; i++)
	{
		// Padding columns on left and right sides of board
		if (i == 0 || i == columns+1)
		{
			for(j = 0; j < rows+2; j++)
				board[i][j] = SPACE;
			continue;
		}
		// Padding Row over bottom of board
		board[i][0] = SPACE;
		for (j = 1; j < rows+1; j++)
		{
			scanf(",%c", &board[i][j]);
			if(board[i][j] < 's')
				p++;
		}
		// Padding row over top of board
		board[i][j] = SPACE;
	}
	return p;
}

void findWins(char board[columns+2][rows+2], char wins[150][4], int last_played)
{
	// last_piece_x and last_piece_y hold co-ordinates of the last played piece, that is the piece that must be checked for wins
	int last_piece_x = last_played, last_piece_y, winIndex = 0;
	int i;
	// get the last piece's Y co-ordinate
	for (i=1;i<rows+1;i++)
	{
		if(board[last_piece_x][i] == SPACE)
			break;
		last_piece_y = i;
	}
	
	//fprintf(f, "Last Moved Piece's co-ordinates:%i, %i, %c\n", last_piece_x, last_piece_y, board[last_piece_x][last_piece_y]);  
	
	/*
	* How this works is: For each directional axis (X, Y, Diagonal Down Left-to-Right, and Diagonal Up Left-to-Right), the last dropped
	* piece can be in 4 vectors (it can be in positions 0-3). For each of these possible positions, the algorithm grabs the 4 pieces
	* and then checks that vector to see if it is a win. If so, it adds it to the wins array.
	*/
	
	// Diagonal Top Left<->Bottom Right
	//fprintf(f, "Beginning Diagonal Loop\n");
	for(i=0;i<4;i++) // Check each of the 4 possible positions for the last dropped piece
	{
		short int offset;
		short int size = 1;
		
		//fprintf(f, "for position:%i\n", i);
		
		// The co-ordinates used to build the vector to check on. Note these move diagonally to the right,down.
		short int start_piece_x = last_piece_x + i;
		short int start_piece_y = last_piece_y - i;
		//fprintf(f, "start_piece co-ordinates are: %i, %i\n", start_piece_x, start_piece_y);
		// If the starting piece is a space, stop moving in that direction
		if (board[start_piece_x][start_piece_y] == SPACE)
		{
			//fprintf(f, "BREAK1: Found Space, finished looking at diagonal!\n", i);
			break;
		}
		char win[4] = {board[start_piece_x][start_piece_y],'s','s','s'};
		// Build a vector going diagonally to the up, left
		for (offset = 1;offset < 4;offset++)
		{
			if (board[start_piece_x-offset][start_piece_y+offset] == SPACE)
			{
				////fprintf(f, "BREAK2: Found Space While building a vector\n");
				break;
			}  
			win[size++] = board[start_piece_x-offset][start_piece_y+offset];
		}
		if (size == 4) // Found a four piece vector (No Spaces)
		{
			////fprintf(f, "Successfully Found a Four-Piece Vector: %s\n", win);
			int k;
			for (k=0;k<12;k++)
			{
				if (strncmp(win,WINS_LOOKUP[k], 4) == 0)
				{
					////fprintf(f, "Found a Win! Vector is %s\n", win);
					strcpy(wins[winIndex++], win);
				}
			}
			
		}
	}
	// Top<->Bottom
	////fprintf(f, "Beginning Vertical Loop\n");
	for(i=0;i<4;i++)
	{
		short int offset, size = 1;
		////fprintf(f, "for position:%i\n", i);
		short int start_piece_x = last_piece_x, start_piece_y = last_piece_y - i;
		////fprintf(f, "start_piece co-ordinates are: %i, %i\n", start_piece_x, start_piece_y);
		if (board[start_piece_x][start_piece_y] == SPACE)
		{
			////fprintf(f, "BREAK1: Found Space, finished looking at Vertical!\n", i);
			break;
		}
		
		char win[4] = {board[start_piece_x][start_piece_y],'s','s','s'};
		// Build a vector going diagonally to the up, left
		for (offset = 1;offset < 4;offset++)
		{
			if (board[start_piece_x][start_piece_y+offset] == SPACE)
			{
				////fprintf(f, "BREAK2: Found Space While building a vector\n");
				break;
			}
			win[size++] = board[start_piece_x][start_piece_y+offset];
		}
		if (size == 4) // Found a four piece vector (No Spaces)
		{
			////fprintf(f, "Successfully Found a Four-Piece Vector: %s\n", win);
			int k;
			for (k=0;k<12;k++)
			{
				if (strncmp(win,WINS_LOOKUP[k], 4) == 0)
				{
					////fprintf(f, "Found a Win! Vector is %s\n", win);
					strcpy(wins[winIndex++], win);
				}
			}
			
		}
	}
	// Diagonal Bottom Left<->Top Right
	////fprintf(f, "Beginning 2nd Diagonal Loop\n");
	for(i=0;i<4;i++)
	{
		short int offset, size = 1;
		////fprintf(f, "for position:%i\n", i);
		
		short int start_piece_x = last_piece_x - i, start_piece_y = last_piece_y - i;
		////fprintf(f, "start_piece co-ordinates are: %i, %i\n", start_piece_x, start_piece_y);
		if (board[start_piece_x][start_piece_y] == SPACE)
		{
			////fprintf(f, "BREAK1: Found Space, finished looking at 2nd diagonal!\n", i);
			break;
		}
		
		char win[4] = {board[start_piece_x][start_piece_y],'s','s','s'};
		// Build a vector going diagonally to the up, left
		for (offset = 1;offset < 4;offset++)
		{
			if (board[start_piece_x+offset][start_piece_y+offset] == SPACE)
			{
				////fprintf(f, "BREAK2: Found Space While building a vector\n");
				break;
			}
			
			win[size++] = board[start_piece_x+offset][start_piece_y+offset];
		}
		if (size == 4) // Found a four piece vector (No Spaces)
		{
			////fprintf(f, "Successfully Found a Four-Piece Vector: %s\n", win);
			int k;
			for (k=0;k<12;k++)
			{
				if (strncmp(win,WINS_LOOKUP[k], 4) == 0)
				{
					////fprintf(f, "Found a Win! Vector is %s\n", win);
					strcpy(wins[winIndex++], win);
				}
			}
			
		}
	}
	//  Left<->Right
	////fprintf(f, "Beginning Horizontal Loop\n");
	for(i=0;i<4;i++)
	{
		short int offset, size = 1;
		////fprintf(f, "for position:%i\n", i);
		
		short int start_piece_x = last_piece_x - i, start_piece_y = last_piece_y;
		////fprintf(f, "start_piece co-ordinates are: %i, %i\n", start_piece_x, start_piece_y);
		if (board[start_piece_x][start_piece_y] == SPACE)
		{
			////fprintf(f, "BREAK1: Found Space, finished looking at Horizontal!\n", i);
			break;
		}
		
		char win[4] = {board[start_piece_x][start_piece_y],'s','s','s'};
		// Build a vector going diagonally to the up, left
		for (offset = 1;offset < 4;offset++)
		{
			if (board[start_piece_x+offset][start_piece_y] == SPACE)
			{
				////fprintf(f, "BREAK2: Found Space While building a vector\n");
				break;
			}
			
			win[size++] = board[start_piece_x+offset][start_piece_y];
		}
		if (size == 4) // Found a four piece vector (No Spaces)
		{
			////fprintf(f, "Successfully Found a Four-Piece Vector: %s\n", win);
			int k;
			for (k=0;k<12;k++)
			{
				if (strncmp(win,WINS_LOOKUP[k], 4) == 0)
				{
					////fprintf(f, "Found a Win! Vector is %s\n", win);
					strcpy(wins[winIndex++], win);
				}
			}
			
		}
	}
	// End with "end of wins" flag vector, to signal the end of wins
	wins[winIndex][0] = END_OF_WINS;
}

// Print Board function for debugging
void printBoard(char board[columns+2][rows+2])
{
	#if DEBUG
	int i, j;
	// TEST: Print out board
	for (i = 0; i < rows+2; i++)
	{
		fprintf(f, "\t");
		for (j = 0; j < columns+2; j++)
		{
			fprintf(f, "%c ", board[j][i]);
		}
		fprintf(f, "\n");
	}
	fprintf(f, "\n");
	#endif
}

int isWin(char board[columns+2][rows+2], int last_played)
{
	char wins[150][4];
	findWins(board, wins, last_played);
	return evalWin(wins);
	
}



/*
*
*	Tested and works correctly
*
*/

int checkCol(char board[columns+2][rows+2], int col)
{  
	int i = 0;
	
	if(board[col][rows] != SPACE)
		return 0;//if the top of the col isn't a space - it is full
	else
		for(i = rows-1; i > 0; i--)
		{
			if(board[col][i] == SPACE)
				continue;
			else
				return i+1;
		}
		return i+1;
}


int checkLoss(char board[columns+2][rows+2])
{
	int i = 0, j = 0, row = 0, score = 0, max = 0, max_move = 0;
	char piece;

	
	for(i = 1; i <= columns; i++)
	{
		
		row = checkCol(board, i);
		
		board[i][row] = RED;
		//rank the moves by the number of points we can get to
		score = isWin(board, i);
#if DEBUG		
//		fprintf(f,"Col: %d, Row: %d, Score: %d\n", i, row, score);
#endif
		
		
		//if we are going to lose right away, place a blue to block;
		if(score < 0)
		{
			printf("(%d, b)", i);
			return 1;
		}
		
		board[i][row] = BLUE;
		//rank the moves by the number of points we can get to
		score = isWin(board, i);
		
#if DEBUG		
		fprintf(f,"Col: %d, Row: %d, Score: %d\n", i, row, score);
#endif
		
		
		//if we are going to lose right away, place a blue to block;
		if(score > 0)
		{
			if(score > max)
			{
				max_move = i;
				max = score;
				piece = BLUE;
			}
		}
		
		board[i][row] = GREEN;
		//rank the moves by the number of points we can get to
		score = isWin(board, i);
		
#if DEBUG		
		fprintf(f,"Col: %d, Row: %d, Score: %d\n", i, row, score);
#endif
		
		board[i][row] = SPACE;
		if(score < 0)
		{
			printf("(%d, b)", i);
			return 1;
		}
		else if(score > max)
		{
			max_move = i;
			piece = GREEN;
			max = score;
		}
		
		
		if(max)
		{
			printf("(%d, %c)", max_move, piece);
		return 1;
		}
	}
	return 0; 
}

int guess(char board[columns+2][rows+2], int col)
{
	int i = 0;
	int m = 0;
	int n = 0;
	int row = 0;
	int score  = 0;
	int x[7] = {col-3, col-2, col -1, col, col+1, col+2, col+3};	

	if((row = checkCol(board, col)) == 0)
		return 0;//col is full
	int y[7] = {row-3, row-2, row-1, row, row+1, row+2, row+3};
	
	for(m = 0; m <= 3; m++)
	{
		if(x[m] < 1 || x[m+3] > columns)//out of bounds - not on board
			continue;
		
		
		score += h_lookup[ (board[x[m]][row] << 3) + (board[x[m+1]][row] << 2) + (board[x[m+2]][row] << 1) + (board[x[m+3]][row])];
		for(n = 0; n <= 3; n++)
		{
			if(y[n] < 1 || y[n+3] > rows)//looking off board, don't do it
				continue;
			
			score += h_lookup[(board[x[m]][y[n]] << 3) + (board[x[m+1]][y[n+1]] << 2) + (board[x[m+2]][y[n+2]] << 1) + (board[x[m+3]][y[n+3]])];//diag up right
			score += h_lookup[(board[x[m]][y[n+3]] << 3) + (board[x[m+1]][y[n+2]] << 2) + (board[x[m+2]][y[n+1]] << 1) + (board[x[m+3]][y[n]])];//diag down right
			score += h_lookup[(board[col][y[n]] << 3) + (board[col][y[n+1]] << 2) + (board[col][y[n+2]] << 1) + (board[col][y[n+3]])];//up-down
			
		}
	}
	#if DEBUG
	//fprintf(f, "guess returns %d in column %d\n", score, col);
	#endif
	return score;
}	
	

			
		
		
	


int max(char board[columns+2][rows+2], int depth, int col)
{
	int best = -5;
	int val = 0;
	int i = 0;
	int row = 0;
	
	if(depth == 0)
		return guess(board, col);
	for(i = col-3; i <= col+3; i++)
	{
		//make sure i is between 1 and columns so we don't start working off the board
		if(i < 1)
			i = 1;
		else if(i >= columns)
			break;
		
		
		if((row = checkCol(board, i)) == 0)
			continue;//col is full - skip the col
		else
		{
			board[i][row] = BLUE;
			
			val = isWin(board, i);
			if(val)
			{
				board[i][row] = SPACE;
				return val<<depth;
			}
			else 
			{
				val = (val<<depth) + min(board, depth-1, i);
				
				if(val > best)
					best = val;
			}
			
			board[i][row] = GREEN;
			
			val = isWin(board, i);
			if(val)//found worst case loss - assume oppenent will choose this
			{
				board[i][row] = SPACE;
				return (val<<depth);
			}
			else 
			{
				val = (val<<depth) + min(board, depth-1, i);
				
				if(val > best)
					best = val;
			}
			
			board[i][row] = SPACE;
			
			
		}		
	}
	#if DEBUG
	for(i = 0; i < depth; i++)
		fprintf(f, "\t");
	fprintf(f, "max returning %d\n", best);
	#endif
	return best;
}

int min(char board[columns+2][rows+2], int depth, int col)
{
	int worst = 5;
	int val = 0;
	int i = 0;
	int row = 0;
	
	if(depth == 0)
		return guess(board, col);
	
	for(i = col-3; i <= col+3; i++)
	{
		//make sure i is between 1 and columns so we don't start working off the board
		if(i < 1)
			i = 1;
		else if(i >= columns)
			break;
		
		
		if((row = checkCol(board, i)) == 0)
			continue;//col is full - skip the col
		else
		{
			board[i][row] = RED;
			
			val = isWin(board, i);
			if(val)//found worst case loss - assume oppenent will choose this
			{
				board[i][row] = SPACE;
				return (val<<(depth+1));
			}
			else 
			{
				val = (val<<(depth+1)) + max(board, depth-1, i);
				
				if(val < worst)
					worst = val;
			}
			
			board[i][row] = GREEN;
			
			val = isWin(board, i);
			if(val)//
			{
				board[i][row] = SPACE;
				return (val<<(depth+1));
			}
			else 
			{
				val = ((val<<depth+1)) + max(board, depth-1, i);
				
				if(val < worst)
					worst = val;
			}
			
			board[i][row] = SPACE;
			
			
		}		
	}
	#if DEBUG
	for(i = 0; i < depth; i++)
		fprintf(f, "\t");
	fprintf(f, "min returning %d\n", worst);
	#endif
	return worst;
}


void makeMove(char board[columns+2][rows+2])
{
	int i = 0;
	int row = 0;
	int val = 0;
	int depth = 7;
	int best = -5;
	int bestcol = 5;
	int leftindent = 1;
	int rightindent = 0;
	char bestcolour = BLUE;
	
//we don't have to look at the whole board if all the pieces are in a few cols
	if(board[1][1] != 's');
	else
		{
		for(i = 1; i < columns; i++)
			if(board[i+1][1] == 's')
				leftindent++;
			else 
				break;
	}
	if(board[columns][1] != 's');
	else
	{
		for(i = columns; i > 0; i--)
			if(board[i-1][1] == 's')
				rightindent++;
			else
				break;
	}	
	
	for(i = leftindent; i<=(columns-rightindent); i++)
	{
		
		if((i - last_move) > 2 || (i - last_move) < -2)
			depth -= 2;//look deeper closer to where the opponent is playing
		if((i - last_move) > 4 || (i - last_move) < -4)
			depth -= 2;//look deeper closer to where the opponent is playing
		if((row =checkCol(board, i)) == 0)
			continue;//col is full
		else
		{
			//place a piece on the board to start looking because max and min only return point values
			board[i][row] = BLUE;
			val = min(board-1, depth, i);
			if(val > best)
			{
				best = val;
				bestcol = i;
				bestcolour = BLUE;
			}
			
		#if DEBUG
		fprintf(f, "Trying col: %d - Piece: Blue - SCORE: %d - Best Column is: %d\n\n", i, val, bestcol);
		printBoard(board);
		#endif
			board[i][row] = GREEN;
			val = min(board, depth, i);
			if(val > best)
			{
				best = val;
				bestcol = i;
				bestcolour = GREEN;
			}
			
		#if DEBUG
		fprintf(f, "Trying col: %d - Piece: Green - SCORE: %d - Best Column is: %d\n\n", i, val, bestcol);
		printBoard(board);
		#endif
			board[i][row] = SPACE;//remove the piece from the board when we are done
			if(best > (5<<depth))//found best possible move, we are done
				break;
			
		}
		
		depth = 6;	
	}

	
	printf("(%d, %c)", bestcol, bestcolour);
	
	
}




int main( int argc, char **argv)
{
	
	int p = 0;
	#if DEBUG
	char fname[32];
	#endif
	
	scanf("(%d,%d,%d,%d,%d,%d", &columns, &rows, &last_move, &total_time, &player_1_time, &last_move_time);
	#if DEBUG
	sprintf(fname, "./moves/game.out.%d.txt", player_1_time);
	f = fopen(fname, "w");
	#endif
	char board[columns+2][rows+2];
	p = readBoard(board);
	printBoard(board);


	/*********************************************/
	// For Debugging the Heuristic -Nick
	#if DEBUG
//	fprintf(f, "GUESS SCORE IS: %i\n", guess(board));
	
	//return 0;
	#endif
	/********************************************/
	if(checkLoss(board))
	{
		#if DEBUG
		fclose(f);
		#endif
		return 0;
	}
	
	
	if(p == 0)
	{
		printf("(5, b)");
		#if DEBUG
		fclose(f);
		#endif
		return 0;
	}
	else if(p < 2)
	{
		if(last_move == 10)
			last_move-=2;
		printf("(%d, b)", last_move+1);
		
		#if DEBUG
		fclose(f);
		#endif
		return 0;
	}

	  
	printBoard(board);
	makeMove(board);
	#if DEBUG
	fclose(f);
	#endif
	return 0;
}
